using System;
using System.Threading.Tasks;
using Nekonya.UI;
using TinaX;
using TinaX.I18N;
using TinaX.I18N.Const;
using TinaX.UIKit;
using UnityEngine;

namespace Nekonya.Managers
{
    public class GlobalManager : IGlobalManager
    {
        [Inject]
        public IUIKit UIKit { get; set; }

        [Inject]
        public II18N I18N { get; set; }


        public void QuitGame()
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#endif
            Application.Quit();
        }

        /// <summary>
        /// 通用弹窗
        /// 简化：只有正文内容和标题，包含一个title
        /// </summary>
        /// <param name="content"></param>
        /// <param name="title"></param>
        public Task ShowMessageBox_1Btn(string content, string title = null, string confirmBtnName = null, bool useMask = true, bool closeByMask = true)
        {
            return UIKit.OpenUIAsync("common.msgbox",
            new MessageBox(),
            new OpenUIParam
            {
                UseMask = useMask,
                CloseByMask = closeByMask,
            },
            new MessageBox.OpenMessageBoxParams
            {
                Title = title,
                Content = content,
                ShowCloseButton = true,
                CloseByKeyboard = true,
                CloseByHandle = true,
                Buttons = new MessageBox.MsgBoxButton[]
                {
                    new MessageBox.MsgBoxButton
                    {
                        Title = confirmBtnName.IsNullOrEmpty() ? this.I18N.GetText("txt_confirm",I18NConst.DefaultGroupName,"Confirm"): confirmBtnName,
                        Style = MessageBox.BtnStyle.Emphasize,
                        Close = true
                    },
                },
            });
        }

        /// <summary>
        /// 通用弹窗
        /// 简化的双按钮（确认/取消）弹窗
        /// </summary>
        /// <param name="content">正文， 支持富文本</param>
        /// <param name="title">标题</param>
        /// <param name="confirmBtnName">确认按钮名称</param>
        /// <param name="cancelBtnName">取消按钮名称</param>
        /// <param name="onConfirm">确认事件</param>
        /// <param name="onCancel">取消事件</param>
        /// <param name="useMask">使用遮罩， 默认启用</param>
        /// <param name="closeByMask">点击遮罩关闭，默认关闭</param>
        /// <param name="priorityConfirmation">优先的“确认”，如果为true，则优先显示“确认”按钮（比如统一放在一个方向，比如加着重色）</param>
        /// <param name="warningConfirmation">警告色的“确认”，如果为true, 则“确认”按钮使用警告色主题</param>
        /// <returns></returns>
        public Task ShowMessageBox_2Btn(string content, string title = null,
            string confirmBtnName = null, string cancelBtnName = null,
            Action onConfirm = null, Action onCancel = null,
            bool useMask = true, bool closeByMask = false,
            bool priorityConfirmation = true, bool warningConfirmation = false)
        {
            //确认按钮
            var btnInfo_confirm = new MessageBox.MsgBoxButton
            {
                Title = confirmBtnName.IsNullOrEmpty() ? this.I18N.GetText("txt_confirm", I18NConst.DefaultGroupName, "Confirm") : confirmBtnName,
                Close = true,
                OnClick = onConfirm
            };
            if (warningConfirmation)
                btnInfo_confirm.Style = MessageBox.BtnStyle.Warning;
            else
                btnInfo_confirm.Style = priorityConfirmation ? MessageBox.BtnStyle.Emphasize : MessageBox.BtnStyle.Normal;

            //取消按钮
            var btnInfo_cancel = new MessageBox.MsgBoxButton
            {
                Title = cancelBtnName.IsNullOrEmpty() ? this.I18N.GetText("txt_cancel", I18NConst.DefaultGroupName, "Cancel") : cancelBtnName,
                Style = priorityConfirmation ? MessageBox.BtnStyle.Normal : MessageBox.BtnStyle.Emphasize,
                Close = true,
                OnClick = onCancel
            };

            return UIKit.OpenUIAsync("common.msgbox",
            new MessageBox(),
            new OpenUIParam
            {
                UseMask = useMask,
                CloseByMask = closeByMask,
            },
            new MessageBox.OpenMessageBoxParams
            {
                Title = title,
                Content = content,
                ShowCloseButton = false,
                CloseByKeyboard = true,
                CloseByHandle = true,
                OnCancel = onCancel,
                Buttons = priorityConfirmation ? new MessageBox.MsgBoxButton[] { btnInfo_confirm, btnInfo_cancel } : new MessageBox.MsgBoxButton[] { btnInfo_cancel, btnInfo_confirm }
            });
        }
    }
}
